package com.myzk;

import com.myzk.config.MyWatcher;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/31  19:19
 * 描述:
 */
public class TestZookeper {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        ZooKeeper zooKeeper=new ZooKeeper("127.0.0.1:2181",5000,null);

       //在根目录下创建目录
       // String s = zooKeeper.create("/PPPP", "123456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
       // System.out.println(s);

        //创建子目录
        /*zooKeeper.getChildren("/PPPP",new MyWatcher());
        String p1 = zooKeeper.create("/PPPP/P2", "123456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("===="+p1);*/


        //更改节点的数据
        /*Stat stat = new Stat();
        byte[] data = zooKeeper.getData("/PPPP", new MyWatcher(),stat);
        System.out.println("======="+new String(data));
        Stat stat1 = zooKeeper.setData("/PPPP", "456789".getBytes(), stat.getVersion());
        */

        //获取一个节点所有的子节点
        /*List<String> children = zooKeeper.getChildren("/PPPP", new MyWatcher());
        children.forEach(path->{
            System.out.println(path);
        });
        String p1 = zooKeeper.create("/PPPP/P3", "123456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);*/


        //判断节点是不是存在
       /* Stat exists = zooKeeper.exists("/PPPP1", new MyWatcher());
        System.out.println(exists==null);
        String p1 = zooKeeper.create("/PPPP1", "123456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);*/







    }

}
